<template>
  <el-dialog :title="title" :visible.sync="open" width="90%" append-to-body :close-on-click-modal="false" v-dialogDrag  >
    <div style="margin-left: 10px;">
      <div style=" display: flex;align-items: center;margin-bottom: 10px;margin-top: 10px;">
      </div>

      <el-form :model="queryParams" ref="queryForm" :inline="true" size="small" v-show="showSearch" label-width="98px"
        style="display: flex;align-items: center;">
        <el-form-item label="通知单编号" prop="tongzhiNo">
          <el-input v-model="queryParams.tongzhiNo" placeholder="请输入通知单编号" style="width: 220px;" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="产品编号" prop="nos">
          <el-input v-model="queryParams.nos" placeholder="请输入产品编号" style="width: 220px;" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="产品名称" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入产品名称" style="width: 220px;" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <div style=" display: flex;margin-left: 20px;margin-bottom: 20px;">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </div>
      </el-form>
      <div v-if="selectedRows.length > 0">
        <span>已勾选数量合计: {{selectedRows.reduce((acc, row) => acc + row.fahuoCount, 0)}}</span>
      </div>
        <el-table :data="columns" ref="dragTable" row-key="id" v-loading="loading" @sort-change="handleSortChange" show-summary :summary-method="totalOutYear" @selection-change="handleSelectionChange" >
          <el-table-column type="selection" width="55" align="center"/>
          <el-table-column sortable label="通知单编号" align="center" prop="tongzhiNo" show-overflow-tooltip />
          <el-table-column sortable label="项目编号" align="center" prop="proNo" />
          <el-table-column sortable label="项目名称" align="center" prop="prodName" show-overflow-tooltip />
          <el-table-column sortable label="产品编码" align="center" prop="nos" />
          <el-table-column sortable label="产品名称" align="center" prop="name"  />
          <el-table-column sortable label="产品型号" align="center" prop="models" />
          <el-table-column sortable label="单位" align="center" prop="danwei" />
          <el-table-column sortable label="单价" align="center" prop="danjian" />
          <el-table-column sortable label="总数量" align="center" prop="shuliang" />
          <el-table-column sortable label="发货数量" align="center" prop="fahuoCount" />
          <el-table-column sortable label="剩余发货数量" align="center" prop="shengyuCount">
            <template slot-scope="scope">
              <span style="color: red; font-weight: bold;" v-if="scope.row.shengyuCount>0">
                {{ scope.row.shengyuCount }}
              </span>
              <span v-if="scope.row.shengyuCount<=0">
                {{ scope.row.shengyuCount }}
              </span>
            </template>
          </el-table-column>
        </el-table>

    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import {
    getAction,
    accAdd,
    getById
  } from '@/api/manage'
  export default {
    dicts: ['yifahuo_status'],
    name: "TongzhidanList",
    components: {},
    data() {
      return {
        title: "发货通知单明细",
        open: false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        rows: [],
        rules: {},
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 销售信息管理表格数据
        dataList: [],
        columns: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 20000,
          nos: null,
          name: null,
          tongzhiId: null,
          tongzhiNo: null,
          orderByColumn: "shengyuCount",
          isAsc: "desc",
          params: {}
        },
        url: {
          list: '/lvFahuomingxi/lvFahuomingxi/list',
        },
        selectedRows:[],
        tableMaxHeight: window.innerHeight - 320,
      };
    },
    updated() {
    this.$nextTick(() => {
    // tab 绑定ref 属性
      this.$refs["dragTable"].doLayout();
    });
  },
    created() {
      // this.intDictsForPrj();
      window.addEventListener('resize', this.getTableHeight);
    },
    destroyed() {
      window.removeEventListener('resize', this.getTableHeight);
    },
    methods: {
      // 多选框选中数据
    handleSelectionChange(selection) {
      this.selectedRows = selection;
    },
    totalOutYear(param) {
      const {columns, data} = param;
      const sums = [];

      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        // 只合计amount1和amount3，想合并哪些列就在这里指定
        if ( column.property === "shengyuCount" || column.property === "fahuoCount" || column.property === "shuliang") {

          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return accAdd(prev , curr);
            } else {
              return prev;
            }
          }, 0);
        }
      });
      return sums;
    },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      getTableHeight() {
        this.tableHeight = window.innerHeight - 320
      },
      showTongzhidanList(row) {
        this.queryParams.tongzhiId = row.id;
        this.open = true;
        this.getList();
      },
      /** 查询销售信息管理列表 */
      getList() {
        this.loading = true;
        getAction(this.url.list, this.queryParams).then(response => {
          this.columns = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 排序触发事件 */
      handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.handleQuery();
    },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.queryParams.params = {}
        this.queryParams.kehuId = ''
        this.resetForm("queryForm");
        this.handleQuery();
      },
    }
  };
</script>
